<?php
	if(isset($_SESSION["id"]))
	{
		if(!isset($_GET["id"]) && !isset($_POST['chk']))
		{
			redirect("Vous n'avez selectionné aucun fil de discution, suppresion impossible !",getLienModule("messagerie","index"));
		}else if(!isset($_GET["id"]) && isset($_POST['chk']))
		{
			$post = (isset($_POST['chk'])) ? $_POST['chk']:NULL; 
			
			if($post != NULL)
			{
				foreach($_POST['chk'] as $v)
				{
					$req = $pdo->prepare("SELECT * FROM forum_mp WHERE mp_id=?");
					$req->execute(array($v));
					$data = $req->fetch();
					
						if(empty($data['mid_delete_post']))
						{
							$req = $pdo->prepare("UPDATE forum_mp SET mid_delete_post=? WHERE mp_id=?");
							$req->execute(array($_SESSION["id"],$v));
						}else{
							$req = $pdo->prepare("DELETE FROM forum_mp WHERE mp_id=?");
							$req->execute(array($v));
							
							$req = $pdo->prepare("DELETE FROM forum_mp_text WHERE mp_post_id=?");
							$req->execute(array($v));
						}
				}
				
				redirect("Opération effectuée",getLienModule("messagerie","index"));
			}else{
				redirect("Opération annulée : Aucun méssages selectionnés",getLienModule("messagerie","index"));	
			}
			
		}else if(isset($_GET['id'])){
			$sur = (isset($_GET["sur"])) ? $_GET["sur"] : -1;
			switch($sur){
				case 0 :
					redirect("Opération annulée",getLienModule("messagerie","index"));
				break;
				
				case 1 :
				if(isset($_GET['id']))
				{
					$v = $_GET['id'];
					
					$req = $pdo->prepare("SELECT * FROM forum_mp WHERE mp_id=?");
					$req->execute(array($v));
					$data = $req->fecth();
					
						if(empty($data['mid_delete_post']))
						{
							$req = $pdo->prepare("UPDATE forum_mp SET mid_delete_post=? WHERE mp_id=?");
							$req->execute(array($_SESSION["id"],$v));
						}else{
							$req = $pdo->prepare("DELETE FROM forum_mp WHERE mp_id=?");
							$req->execute(array($v));
							
							$req = $pdo->prepare("DELETE FROM forum_mp_text WHERE mp_post_id=?");
							$req->execute(array($v));
						}
				}else
					redirect("Erreur technique",getLienModule("messagerie","index"));
				break;
				
				default :
					redirect("Erreur technique",getLienModule("messagerie","index"));
				break;
			}
		}

	}
	

?>